CREATE    trigger tft_mstrader_bi on dbo.tft_mstrader 
instead of insert
as
begin   
  set nocount on
  declare  @nbalyear integer   
  declare  @nbalmonth integer   
   
  declare  @bmbala decimal(18,2)   
  declare  @bysumdebit decimal(18,2)   
  declare  @bysumcredit decimal(18,2)   
  declare  @tmsumdebit decimal(18,2)   
  declare  @tmsumcredit decimal(18,2)   
  declare  @tmsum decimal(18,2)   
   
  declare  @fbmbala decimal(18,2)   
  declare  @fbysumdebit decimal(18,2)   
  declare  @fbysumcredit decimal(18,2)   
  declare  @ftmsumdebit decimal(18,2)   
  declare  @ftmsumcredit decimal(18,2)   
  declare  @ftmsum decimal(18,2)   
   
   
  declare  @nsubjectid integer
  declare  @ntraderid integer
  declare  @nyear integer   
  declare  @nmonth integer   
  declare  @nfilid integer   
  declare  @nmoneyid integer   
  declare  @nbmbala decimal(18,2)   
  declare  @nbysumdebit decimal(18,2)   
  declare  @nbysumcredit decimal(18,2)   
  declare  @ntmsumdebit decimal(18,2)   
  declare  @ntmsumcredit decimal(18,2)   
  declare  @ntmsum decimal(18,2)   
  declare  @nfbmbala decimal(18,2)   
  declare  @nfbysumdebit decimal(18,2)   
  declare  @nfbysumcredit decimal(18,2)   
  declare  @nftmsumdebit decimal(18,2)   
  declare  @nftmsumcredit decimal(18,2)   
  declare  @nftmsum decimal(18,2)   
  declare  @nodata integer   
  declare @local_tab table(
	[traderid] int  not null ,
	[subjectid] int  not null ,
	[nyear] int not null ,
	[nmonth] int not null ,
        [filid] int not null,
        [moneyid] int not null,
	[bmbala]    decimal(18,2),
	[bysumdebit]   decimal(18,2),
	[bysumcredit]  decimal(18,2),
	[tmsumdebit]  decimal(18,2),
	[tmsumcredit]   decimal(18,2),
	[tmsum]  decimal(18,2),
	[fbmbala]   decimal(18,2),
	[fbysumdebit]   decimal(18,2),
	[fbysumcredit]   decimal(18,2),
	[ftmsumdebit]   decimal(18,2),
	[ftmsumcredit]  decimal(18,2),
	[ftmsum]  decimal(18,2),
        [odata] int)

  insert into @local_tab select * from inserted
  declare cur_mstrader cursor for select * from @local_tab

  open cur_mstrader 
  fetch next from  cur_mstrader
  into @ntraderid, @nsubjectid,@nyear, @nmonth,@nfilid,@nmoneyid, @nbmbala , @nbysumdebit, @nbysumcredit,
        @ntmsumdebit,@ntmsumcredit,@ntmsum, @nfbmbala, @nfbysumdebit, @nfbysumcredit,
        @nftmsumdebit , @nftmsumcredit, @nftmsum ,@nodata
  while @@fetch_status = 0 
  begin    
    set  @nbalyear=NULL
    set  @nbalmonth=NULL
    set  @bmbala = 0
    set  @bysumdebit = 0
    set  @bysumcredit = 0
    set  @tmsumdebit = 0
    set  @tmsumcredit = 0
    set  @tmsum = 0
    set  @fbmbala = 0
    set  @fbysumdebit = 0
    set  @fbysumcredit = 0
    set  @ftmsumdebit = 0
    set  @ftmsumcredit = 0
    set  @ftmsum = 0

    select @nbalyear = nbalyear,
           @nbalmonth = nbalmonth,
           @bmbala = bmbala,
           @bysumdebit = bysumdebit,
           @bysumcredit = bysumcredit,
           @tmsumdebit = tmsumdebit,
           @tmsumcredit = tmsumcredit,
           @tmsum = tmsum,
           @fbmbala = fbmbala,
           @fbysumdebit = fbysumdebit,
           @fbysumcredit = fbysumcredit,
           @ftmsumdebit = ftmsumdebit,
           @ftmsumcredit = ftmsumcredit,
           @ftmsum = ftmsum
      from f_mstradergetpriorinfo(@nmoneyid,@ntraderid,@nsubjectid,@nyear,@nmonth,@nfilid)
   
    if ( @bmbala is null)       set @bmbala =0   
    if ( @bysumcredit is null)  set @bysumcredit =0   
    if ( @bysumdebit is null)   set @bysumdebit =0   
    if ( @tmsumcredit is null)  set @tmsumcredit =0   
    if ( @tmsumdebit is null)   set @tmsumdebit =0   
   
    set @nbysumcredit=@tmsumcredit + @bysumcredit   
    set @nbysumdebit =@tmsumdebit  + @bysumdebit    
   
    set @nbmbala=@bmbala+@tmsumdebit-@tmsumcredit   
  
    if (@fbmbala is null)       set @fbmbala =0   
    if (@fbysumcredit is null)  set @fbysumcredit =0   
    if (@fbysumdebit is null)   set @fbysumdebit =0   
    if (@ftmsumcredit is null)  set @ftmsumcredit =0   
    if (@ftmsumdebit is null)   set @ftmsumdebit =0   
      
    set @nfbysumcredit=@ftmsumcredit + @fbysumcredit   
    set @nfbysumdebit =@ftmsumdebit  + @fbysumdebit    
       
    set @nfbmbala=@fbmbala+@ftmsumdebit-@ftmsumcredit   
   
    if (@nyear<>@nbalyear)    
    begin   
      set @nbysumcredit=0   
      set @nbysumdebit=0   
      set @nfbysumcredit=0   
      set @nfbysumdebit=0   
   end
   insert into tft_mstrader values( @ntraderid,@nsubjectid, @nyear, @nmonth,@nfilid, @nmoneyid, isnull(@nbmbala,0) , isnull(@nbysumdebit,0),
     isnull(@nbysumcredit,0), isnull(@ntmsumdebit,0),isnull(@ntmsumcredit,0),isnull(@ntmsum,0), isnull(@nfbmbala,0),
     isnull(@nfbysumdebit,0),isnull(@nfbysumcredit,0),isnull(@nftmsumdebit,0) , isnull(@nftmsumcredit,0), 
     isnull(@nftmsum,0),@nodata)
  fetch next from  cur_mstrader
  into  @ntraderid,@nsubjectid, @nyear, @nmonth,@nfilid, @nmoneyid,@nbmbala , @nbysumdebit, @nbysumcredit,
       @ntmsumdebit,@ntmsumcredit,@ntmsum, @nfbmbala, @nfbysumdebit, @nfbysumcredit,
       @nftmsumdebit , @nftmsumcredit, @nftmsum ,@nodata
  end
  close cur_mstrader
  deallocate cur_mstrader
end

